﻿using ThresholdAssistant.Autofac;
using ThresholdAssistant.Http.Extensions;
using Polly;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace ThresholdAssistant.Http
{
    /// <summary>
    /// Polly客户端工具类
    /// </summary>
    public class PollyClientHelp : IBaseAutofac
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly IHttpClientFactory factory;

        /// <summary>
        /// 注入构造函数
        /// </summary>
        /// <param name="_actory"></param>
        public PollyClientHelp(IHttpClientFactory _actory)
        {
            factory = _actory;
        }

        /// <summary>
        /// GET请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<string> GetAsync(string url, Dictionary<string, string> paramters = null, Dictionary<string, string> headers = null, int timeoutSeconds = 3)
        {
            IAsyncPolicy policy = url.GetPolicySingle();
            return await policy.ExecuteAsync(() => factory.GetAsync<string>(url, paramters, headers, timeoutSeconds));
        }

        /// <summary>
        /// GET请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<T> GetAsync<T>(string url, Dictionary<string, string> paramters = null, Dictionary<string, string> headers = null, int timeoutSeconds = 3)
        {
            IAsyncPolicy policy = url.GetPolicySingle();
            return await policy.ExecuteAsync(() => factory.GetAsync<T>(url, paramters, headers, timeoutSeconds));
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<string> PostAsync(string url, string paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            IAsyncPolicy policy = url.GetPolicySingle();
            return await policy.ExecuteAsync(() => factory.PostAsync<string, string>(url, paramters, headers, contentType, timeoutSeconds));
        }

        /// <summary>
        /// 发送Post请求
        /// </summary>
        /// <typeparam name="R">请求模型</typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<string> PostAsync<R>(string url, R paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            IAsyncPolicy policy = url.GetPolicySingle();
            return await policy.ExecuteAsync(() => factory.PostAsync<R, string>(url, paramters, headers, contentType, timeoutSeconds));
        }

        /// <summary>
        /// 发送Post请求
        /// </summary>
        /// <typeparam name="T">返回模型</typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<T> PostAsync<T>(string url, string paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            IAsyncPolicy policy = url.GetPolicySingle();
            return await policy.ExecuteAsync(() => factory.PostAsync<string, T>(url, paramters, headers, contentType, timeoutSeconds));
        }

        /// <summary>
        /// 发送Post请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="paramters"></param>
        /// <param name="headers"></param>
        /// <param name="contentType"></param>
        /// <param name="timeoutSeconds">超时时间(单位：秒)</param>
        /// <returns></returns>
        public async Task<T> PostAsync<R, T>(string url, R paramters, Dictionary<string, string> headers = null, string contentType = "application/json", int timeoutSeconds = 3)
        {
            IAsyncPolicy policy = url.GetPolicySingle();
            return await policy.ExecuteAsync(() => factory.PostAsync<R, T>(url, paramters, headers, contentType, timeoutSeconds));
        }
    }
}